//
//  WFWenBaViewController.m
//  WangyiFangzuo
//
//  Created by 瑜 on 16/9/12.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFWenBaViewController.h"
#import "WFXiaLaTableViewCell.h"
#import <AFNetworking.h>
#import "WFWebBaModel.h"
#import "WFWenBaTableViewCell.h"
#import <MJRefresh.h>
@interface WFWenBaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrM;
@property(nonatomic,assign)int height;
@end

@implementation WFWenBaViewController
- (NSMutableArray *)arrM
{
    if(!_arrM)
    {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrM.count+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        static NSString *ID = @"cell";
        WFXiaLaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if(!cell)
        {
            cell = [WFXiaLaTableViewCell cellWithArray:nil];
        }
        return cell;
    }
    else
    {
        static NSString *ID = @"Cell";
        WFWenBaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if(!cell)
        {
            cell = [WFWenBaTableViewCell cell];
        }
        WFWebBaModel *model = self.arrM[indexPath.section-1];
        cell.model = model;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return _height;
    }
    else
    {
        return 280;
    }
    
}
- (void)loadData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager GET:@"http://c.m.163.com/newstopic/list/expert/5YWo5Zu9/0-10.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *arrr = [[NSMutableArray alloc] init];
        for(NSDictionary *dict in responseObject[@"data"][@"expertList"])
        {
            WFWebBaModel *model = [WFWebBaModel modelWithDict:dict];
            [arrr addObject:model];
        }
        self.arrM = arrr;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(section == 0)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 20);
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        return btn;
    }
    else
    {
        return nil;
    }
}
- (void)click:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if(sender.selected)
    {
        _height = 80;
    }
    else
    {
        _height = 200;
    }
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index, nil] withRowAnimation:UITableViewRowAnimationNone];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 30;
    }
    else
    {
        return 5;
    }
}

@end
